package com.xingguo.show;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.base.component.BaseAction;
import com.base.component.BaseHibernateDAO;
import com.base.component.Constant;
import com.base.component.MessageConstant;
import com.base.component.Pager;
import com.opensymphony.xwork2.ModelDriven;
import com.xingguo.account.Account;
import com.xingguo.account.AccountAction;
@Controller
public class ShowAction extends BaseAction implements ModelDriven<Show> {
	@Resource
	BaseHibernateDAO baseHibernateDAO;
	
	private Pager pager = new Pager();
	private Integer currentId;
	private String[] imgNameList;
	private String[] imgURLList;
	private String[] idArray;
	public Show show = new Show();
	
	static Logger logger = LogManager.getLogger(AccountAction.class.getName());
	@SuppressWarnings("unchecked")
	public String search(){
		String title = "";
		title = pager.getValues()[0];
		String hql = "from Show a where a.title like '%"+title+"%'";
		pager.setProps(new String[]{"title"});
		Long totalSize = baseHibernateDAO.countByHQL("select count(*)"+hql);
		pager.setTotalSize(totalSize);
		System.out.println(pager);
		int offSet = pager.getOffset();
		int pageSize = pager.getSize();
		List<Account> list = baseHibernateDAO.findByHQL(hql, offSet, pageSize);
		return addAttr("list", list).forward("list.jsp");
	}

	private HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}
	private List<ShowImage> getImages(){
		
		List<ShowImage> imgList = new ArrayList<ShowImage>();
		if(null == imgNameList){
			return imgList;
		}
		int imgCount = imgNameList.length;
		for(int i=0;i<imgCount;i++){
			ShowImage image = new ShowImage();
			image.setImgName(imgNameList[i]);
			image.setUrl(imgURLList[i]);
			imgList.add(image);
		}
		return imgList;
	}
	
	public String goToAdd(){
		Show tempShow = new Show();
		return addAttr("tempShow", tempShow).forward("edit.jsp");
	}
	
	public String add(){
		show.setAddDate(new Date());
		show.setImages(getImages());
		baseHibernateDAO.save(show);
		logger.info("add:{}", show);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.SAVE_SUCCESS);
		return search();
	}

	public String goToEdit(){
		Show tempShow = (Show) baseHibernateDAO.get(Show.class, currentId);
		return addAttr("tempShow", tempShow).forward("edit.jsp");
	}
	
	public String edit(){
		show.setImages(getImages());
		baseHibernateDAO.update(show);
		logger.info("edit:{}", show);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.EDIT_SUCCESS);
		return search();
	}
	
	public String delete() {
		baseHibernateDAO.delete(show);
		logger.info("delete:{}",show);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.DELETE_SUCCESS);
		return search();
	}
	
	public String deleteBatch(){
		for(String id:idArray){
			Show tempShow = new Show();
			tempShow.setId(Integer.valueOf(id));
			baseHibernateDAO.delete(tempShow);
		}
		logger.info("deleteBatch:{}",(Object[])idArray);
		getRequest().setAttribute(Constant.MESSAGE, MessageConstant.DELETE_SUCCESS);
		return search();
	}
	
	@Override
	public Show getModel() {
		return show;
	} 
	public String upload(){
		System.out.println(show);
		return null;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public Integer getCurrentId() {
		return currentId;
	}

	public void setCurrentId(Integer currentId) {
		this.currentId = currentId;
	}

	public String[] getImgNameList() {
		return imgNameList;
	}

	public void setImgNameList(String[] imgNameList) {
		this.imgNameList = imgNameList;
	}

	public String[] getImgURLList() {
		return imgURLList;
	}

	public void setImgURLList(String[] imgURLList) {
		this.imgURLList = imgURLList;
	}
	
	public String[] getIdArray() {
		return idArray;
	}

	public void setIdArray(String[] idArray) {
		this.idArray = idArray;
	}
}
